<template>
  <el-dialog
    :close-on-click-modal="false"
    modal-append-to-body lock-scroll top="10%"
    :width="'75%'"
    :title="title"
    :visible.sync="isdealog"
  >
    <div style="height: 460px;">
      <merge
        v-if="isdealog"
        :isDailog="true"
        :isDalog="isdealog"
        :custInfo="customer"
        :bindCustomer="bindCustomer"
        @change-merge="mergeCustomer"
        @closeDialog="close"
      >
      </merge>
    </div>
  </el-dialog>
</template>

<script>
import Merge from '@/components/business-modules/customer/Merge.vue'

function updateCurrentWebchatList (store, sid, currentCustomer) {
  store.dispatch('updateCurrentWebchatListCustomer', {sid: sid, customer: currentCustomer})
}

export default {
  name: 'MergeCustomerDialog',
  components: {
    Merge
  },
  data () {
    return {
      isdealog: false
    }
  },
  props: {
    title: String,
    customer: {
      type: Object,
      default: function () {
        return {}
      }
    },
    bindCustomer: {
      type: Object,
      default: null
    }
  },
  methods: {
    open () {
      this.isdealog = true
    },
    close () {
      this.isdealog = false
    },
    mergeCustomer (val) {
      let tabType = this.$route.path.split('/')[3] || ''
      let sessionId = this.$route.path.split('/')[4]

      let data = {
        customerId: val._id,
        callId: this.$route.params.id,
        custName: val.name,
        customer: val.custInfo,
        submenu: tabType,
        sessionId: sessionId
      }
      this.$store.dispatch('locationCustomer', data).then(() => {
        this.$message({
          type: 'success',
          message: this.$t('customer.succMerge')
        })
        let sid = null
        if (tabType === 'webchat_todo') {
          sid = this.$store.state.webchat.current[tabType].currentSession[sessionId].sid || ''
        } else {
          sid = this.$store.state.webchat.current[tabType].currentSession.sid || ''
        }
        this.close()
        updateCurrentWebchatList(this.$store, sid, val.custInfo)
      })
    }
  }
}
</script>

<style>
</style>
